#ifndef E95E1BF3_6766_4F23_A128_00FE070A66E0
#define E95E1BF3_6766_4F23_A128_00FE070A66E0

#include <iostream>
#include <cstring>
#include <string>
#include <ctime>
#include "Log.hpp"
#include "Init.hpp"

Init init;

extern Log lg;

class Task
{
public:
	Task()
	{
	}
	Task(int sockfd, std::string clientip, uint16_t clientport) : sockfd_(sockfd), clientip_(clientip), clientport_(clientport)
	{
	}
	void run()
	{
		char buffer[4096];

		int n = read(sockfd_, buffer, sizeof(buffer));
		if (n > 0)
		{
			buffer[n] = 0; //'/0'
			std::cout << "client say# " << buffer << std::endl;
			// std::string answer = "server echo# ";
			// answer += buffer;
			
			std::string answer = init.translation(buffer);
			write(sockfd_, answer.c_str(), answer.size());
		}
		else if (n == 0)
		{
			lg(Info, "%s : %d quit", clientip_.c_str(), clientport_);
		}
		else
		{
			lg(Warnning, "%s read error", clientip_.c_str());
		}
		close(sockfd_);
	}
	void operator()()
	{
		run();
	}

private:
	int sockfd_;
	std::string clientip_;
	uint16_t clientport_;
};

#endif /* E95E1BF3_6766_4F23_A128_00FE070A66E0 */
